<?php
namespace app\admin\model;

use think\Model;

class Order extends Model{

    public function searchKeywordsAttr($query,$v){
        if (''!==$v){
            $query->where('ecm_order.buyer_name','like','%'.$v.'%');
        }
    }
    public function searchStatusAttr($query,$v){
        if(isset($v) && $v!=""){
            $query->where('ecm_order.status',$v);
        }
    }
    public function searchSMinAttr($q,$v,$data){
        if (!empty($v)){
            $q->whereTime('ecm_order.add_time','<=',$v);
        }
    }
    public function searchSMaxAttr($q,$v,$data){
        if (!empty($v)){
            $q->whereTime('ecm_order.add_time','>=',$v);
        }
    }
    public function searchMinAttr($q,$v,$data){
        if(isset($v) && $v!=""){
            $q->where('ecm_order.goods_amount','>=',$v);
        }
    }
    public function searchMaxAttr($q,$v,$data){
        if(isset($v) && $v!=""){
            $q->where('ecm_order.goods_amount','<=',$v);
        }
    }
    public static function getOrderList($data=[],$query=[],$page = true)
    {
        $field=[
            'o.order_id','o.buyer_name','o.goods_amount','e.order_status',
            'o.add_time','o.postscript','o.order_sn'
        ];
        $model = self::field($field)
            ->alias('o')->field($field)
            ->withSearch(array_keys($data),$data)
            ->join(['ecm_order_status' =>'e'],'e.order_status=o.status','LEFT');

        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => $list_rows,
                'query'     => $query,
            ]);
        }else{
            return $model->select();
        }
    }
}